package com.pcbly.web.components.license;

import java.io.Serializable;

@SuppressWarnings("unchecked")
public class Version implements Comparable, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3116412951200148148L;
	private int major;
	private int minor;
	private int revision;
	private String extraVersionInfo;

	public int getMajor() {
		return major;
	}

	public int getMinor() {
		return minor;
	}

	public int getRevision() {
		return revision;
	}

	public String getExtraVersionInfo() {
		return extraVersionInfo;
	}

	public String getVersionString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(major);
		buffer.append(".");
		buffer.append(minor);
		buffer.append(".");
		buffer.append(revision);
		if (extraVersionInfo != null) {
			buffer.append(" ");
			buffer.append(extraVersionInfo);
		}
		return buffer.toString();
	}

	public int compareTo(Version version) {
		if (major > version.major)
			return 1;
		if (major < version.major)
			return -1;
		if (minor > version.minor)
			return 1;
		return minor >= version.minor ? 0 : -1;
	}

	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;
		Version version = (Version) o;
		if (major != version.major)
			return false;
		if (minor != version.minor)
			return false;
		if (revision != version.revision)
			return false;
		else
			return extraVersionInfo == null ? version.extraVersionInfo != null
					: !extraVersionInfo.equals(version.extraVersionInfo);
	}

	public int hashCode() {
		int result = major;
		result = 29 * result + minor;
		result = 29 * result + revision;
		result = 29 * result
				+ (extraVersionInfo == null ? 0 : extraVersionInfo.hashCode());
		return result;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("Version");
		sb.append("{major=").append(major);
		sb.append(", minor=").append(minor);
		sb.append(", revision=").append(revision);
		sb.append(", extraVersionInfo='").append(extraVersionInfo).append('\'');
		sb.append('}');
		return sb.toString();
	}

	public static Version parseVersion(String s) {
		if (s == null)
			throw new LicenseException("Version string can not be null");
		Version version = new Version();
		String items[] = s.trim().split("\\.");
		try {
			version.major = Integer.parseInt(items[0]);
			version.minor = Integer.parseInt(items[1]);
			if (items.length > 2)
				if (items[2].indexOf(" ") > -1) {
					version.revision = Integer.parseInt(items[2].substring(0,
							items[2].indexOf(" ")));
					version.extraVersionInfo = items[2].substring(
							items[2].indexOf(" "), items[2].length()).trim();
				} else {
					version.revision = Integer.parseInt(items[2]);
				}
		} catch (NumberFormatException e) {
			throw new LicenseException((new StringBuilder()).append(
					"Illegal version ").append(s).toString());
		}
		return version;
	}

	private Version() {
	}

	public Version(int major, int minor, int release) {
		this.major = major;
		this.minor = minor;
		revision = release;
	}

	public Version(int major, int minor, int release, String versionString) {
		this.major = major;
		this.minor = minor;
		revision = release;
		extraVersionInfo = versionString;
	}

	@Override
	public int compareTo(Object obj) {
		// TODO Auto-generated method stub
		return compareTo((Version) obj);
	}
}
